namespace SimpleWebServices.Web.Routing
{
    using System;

    public sealed class RouteName : ValueObject<RouteName>
    {
        private string _value;

        public RouteName()
        {
            this.RegisterProperty(x => x.Value);
        }

        public RouteName(string value)
        {
            this.Value = value;
        }

        public int Length
        {
            get
            {
                return this.Value.Length;
            }
        }

        public string Value
        {
            get
            {
                return this._value;
            }

            set
            {
                if (null == value)
                {
                    throw new ArgumentNullException("value");
                }
                else if (0 == value.Length)
                {
                    throw new ArgumentOutOfRangeException("value");
                }
                else if (value.StartsWith("/", StringComparison.OrdinalIgnoreCase) || value.EndsWith("/", StringComparison.OrdinalIgnoreCase))
                {
                    throw new FormatException("The name cannot start or end with a delimiter.");
                }
                
                this._value = value;
            }
        }

        public static implicit operator RouteName(string value)
        {
            return (null == value) ? null as RouteName : new RouteName(value);
        }

        public static implicit operator string(RouteName extension)
        {
            return (null == extension) ? null as string : extension.Value;
        }

        public override string ToString()
        {
            return this.Value;
        }
    }
}